<?php
class getUrl{
    private $url;
    public $urlData;
    public function __construct($data){
//        $this->url = $data;
//        $this->urlData = $this->getFormatUrl($this->url);
//        //筛选掉一些重复的网址
//        $this->urlData = array_unique($this->urlData);
//        //筛选掉自定义不需要的网址的内容
//        $this->urlData = $this->getOut($this->urlData);
//        return $this->urlData;
          $this->getUrlContent("http://www.powmo.com/");
    }
    //指定要排除一些域名带特定标记的网站
    private function getOut($arr){
        //$arr must be an array
        $temp = array();
        //记得定义要过滤的网站加个转义符
        $n = array(
           'google\.com','youtube\.com',
        );
//        echo "<pre>"; print_r("'/$n[0]/'");exit;
        foreach($arr as $k=>$v){
            if(!preg_match("/$n[0]/",$v)&&!preg_match("/$n[1]/",$v)){
                $temp[] = $v;
            }
        }
        return $temp;
    }
    //根据页面源码判断网页编码方式
    private function getCharset($str){
        $charSet = mb_detect_encoding($str,array('ASCII','UTF-8','GB2312','GBK','BIG5'));
        return $charSet;
    }
    //得到页面源码
    private function getPageSource($url){
        $url = $this->isRedirectUrl($url);
        if(preg_match('/^https/',$url)){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $pageSource = curl_exec($ch);
//            $reurl = curl_getinfo($ch);
//            echo "<pre>";print_r($reurl);exit;
        }else{
           $pageSource = file_get_contents($url);
        }
//        print_r($pageSource);exit;
        return $pageSource;
    }
//检查是否有重定向,如果重定向,取到重定向后的网址并解码返回
    private function isRedirectUrl($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_exec($ch);
        $reurl = curl_getinfo($ch);
        if(!empty($reurl['redirect_url'])){
            $urls = urldecode($reurl['redirect_url']);
        }else{
            $urls = urldecode($url);
        }
        return $urls;
    }
    //通过网页编码对页面源码进行字符串转化,转化为正常的字符串
    private function covertToArr($url){
        $str = $this->getPageSource($url);
        $charsetInfo = $this->getCharset($str);
        switch($charsetInfo){
            case "EUC-CN":
                $sourceArr = htmlentities($str,ENT_QUOTES,'GB2312');break;
            case "UTF-8":
                $sourceArr = htmlentities($str,ENT_QUOTES,'UTF-8');break;
            case "CP936":
//                print_r($str);
                $sourceArr = htmlentities($str,ENT_QUOTES,'GB2312');break;
            case "ASCII":
                $sourceArr = htmlentities($str,ENT_QUOTES,'UTF-8');break;
            default:
                $sourceArr = '';
        }
        return $sourceArr;
    }
    private function urlArr($url){
        //得到正常的网页源码字符串
        $str = $this->getPageSource($url);
//        $str = $this->getPageSource($url);
//        print_r($str);exit;
        //正则匹配出来所有的链接信息
        preg_match_all("/http:\/\/[\w\.\/]*/",$str,$matches);
//        print_r($matches);exit;
        return $matches;
    }
    //get url's content
    private function getUrlContent($url){
        $str = $this->getPageSource($url);
       preg_match_all('/<title>([\S\s]*?)<\/title>/',$str,$matches);
       preg_match_all('/<body[^>]*?>([\S\s]*?)<\/body>/',$str,$content);
//       print_r($content);exit;
//       $content = htmlentities($content,ENT_QUOTES,'UTF-8');
       $search ="/<script[^>]*?>.*?<\/script>/si";
//       print_r($content);exit;
       $rr = preg_replace($search,' ',$content[0][0]);
       print_r($rr);
    }
    private function getFormatUrl($url){
        $result = array();
        $allUrl = $this->urlArr($url);
        foreach($allUrl as $k=>$v){
            foreach($v as $key=>$value){
                    $result[] = $value;
            }
        }
        return $result;
    }
}
//$str = "http://www.huffingtonpost.com/2012/10/15/cuban-missile-crisis-unto_n_1967544.html";
//$matches = preg_match("/(http:\/\/)*www\.huffingtonpost\.com\/2012\/10\/15/cuban-missile-crisis-unto_n_1967544\.html/i","",$str);
//print_r($matches);
$t = new getUrl("https://www.google.com.hk/search?q=html&hl=zh-CN&safe=strict&biw=1280&bih=657&prmd=imvns&ei=jaqIUJmxNO3TmAXbsIC4Cw&start=30&sa=N");
echo "<pre>";print_r($t->urlData);
?>
<?php
//$str = "/url?q=http://www.w3schools.com/html/default.asp&sa=U&ei=zKOgUOjdBK_BiQey4IBI&ved=0CB8QFjAA&usg=AFQjCNFtgmXvRk4uLQpDoWwhd38EDBb-EA";
//$str = "<title>HTML Formsand Input</title>";
//preg_match_all('/<title>([\S\s]*?)<\/title>/',$str,$data);
//echo "<pre>";print_r($data);
//$str = '';
//$arr = array('1','2','3','4','5','6','7','8','9','0','a','b','c','d'
//,'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u'
//,'v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L'
//,'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
//for($i=0;$i<=8;$i++){
//$str .=$arr[rand(0,61)];
//}
//
//echo $str;
//echo "<br/>";
//echo md5($str);
?>